package com.netease.nie.controllers;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.netease.nie.base.controllers.BaseController;
import com.netease.nie.constants.BusinessConstants;
import com.netease.nie.dto.AddressDeleteDto;
import com.netease.nie.dto.AddressDto;
import com.netease.nie.dto.AdminProvinceDto;
import com.netease.nie.dto.ChooseAddrDto;
import com.netease.nie.service.AddressService;

/**
 * 收货地址管理接口
 * @author Derek Xu
 * @CreateDate Aug 6, 2016
 */
@RestController
@RequestMapping("/api/user")
public class AddressController extends BaseController {
	
	@Autowired
	private AddressService addressService;
	
	@RequestMapping(value = "/list_address", method = RequestMethod.GET, produces = "application/json")
	public String list(HttpServletRequest request) {
		Long userId = (Long) request.getAttribute("userId");
		if (userId == null)
			return jsonFail(BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
		List<AddressDto> addressDtoList = addressService.listAddressByUserId(userId);
		return jsonSuccess(addressDtoList);
	}
	
	@RequestMapping(value = "/default_address", method = RequestMethod.GET, produces = "application/json")
	public String defaultAddress(HttpServletRequest request) {
		Long userId = (Long) request.getAttribute("userId");
		if (userId == null)
			return jsonFail(BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
		AddressDto addressDto = addressService.queryDefaultAddressByUserId(userId);
		return jsonSuccess(addressDto);
	}
	
	@RequestMapping(value = "/edit_address", method = RequestMethod.GET, produces = "application/json")
	public String edit(HttpServletRequest request, Long id) {
		Long userId = (Long) request.getAttribute("userId");
		if (userId == null)
			return jsonFail(BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
		AddressDto addressDto = addressService.queryAddressById(id, userId);
		return jsonSuccess(addressDto);
	}
	
	@RequestMapping(value = "/save_address", method = RequestMethod.POST, produces = "application/json")
	public String save(HttpServletRequest request, @RequestBody AddressDto addressDto) {
		Long userId = (Long) request.getAttribute("userId");
		if (userId == null)
			return jsonFail(BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
		
		addressDto.setUserId(userId);
		int result = addressService.saveAddress(addressDto, userId);
		return jsonSuccess(result);
	}
	
	@RequestMapping(value = "/delete_address", method = RequestMethod.POST, produces = "application/json")
	public String delete(HttpServletRequest request,@RequestBody AddressDeleteDto addressDeleteDto) {
		Long userId = (Long) request.getAttribute("userId");
		if (userId == null)
			return jsonFail(BusinessConstants.USER_NOT_LOGIN.getCode(), "用户未登录");
		Long addressId = addressDeleteDto.getId();
		int result = addressService.deleteAddressById(addressId, userId);
		return jsonSuccess(result);
	}
	
	@RequestMapping(value = "/choose_address", method = RequestMethod.POST, produces = "application/json")
	public String chooseAddr(@RequestBody ChooseAddrDto chooseAddrDto) throws Exception {
		AdminProvinceDto adminProvinceDtos = addressService.queryAdminProvinceDtos(chooseAddrDto);
		return jsonSuccess(adminProvinceDtos);
	}
}
